package spotGUI.ElementViewer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;

import managers.socialNetworkManagers.ElementsManager;
import personalProfile.PersonalProfile;
import spotGUI.utils.EmptyContentAreaButton;
import elements.Element;



public class ElementView extends JPanel {


	private JLabel lblPostTypeIcon;
	private JButton btnSender;
	private JLabel lblPostPhoto;
	private JLabel lblTitle;
	private JTextArea textArea;
	private JLabel lblDate;
	private JButton btnSeleziona;
	private JLabel lblUsername;
	private JScrollPane scrollPane;

	private Element element;
	private ElementPanel elementPanel;

	private ActionListener openProfileListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			new PersonalProfile(element.userId);
		}
	};

	private ActionListener selectElementListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			elementPanel.setSelectedElement(element);
		}
	};


	public ElementView(Element element, ElementPanel elementPanel) {

		setPreferredSize(element);

		setBorder(new CompoundBorder(new EmptyBorder(7, 0, 0, 0), new MatteBorder(5, 0, 0, 0, (Color) new Color(233, 199, 5))));

		this.element = element;
		this.elementPanel = elementPanel;

		setAlignmentY(Component.TOP_ALIGNMENT);
		setMinimumSize(new Dimension(500, 230));
		setAlignmentX(Component.LEFT_ALIGNMENT);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{32, 15, 239, 131, 29, 0};
		gridBagLayout.rowHeights = new int[]{28, 14, 37, 18, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		lblPostTypeIcon = new JLabel("");
		GridBagConstraints gbc_lblPostTypeIcon = new GridBagConstraints();
		gbc_lblPostTypeIcon.insets = new Insets(0, 0, 5, 5);
		gbc_lblPostTypeIcon.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblPostTypeIcon.gridx = 0;
		gbc_lblPostTypeIcon.gridy = 0;
		add(lblPostTypeIcon, gbc_lblPostTypeIcon);

		lblPostTypeIcon.setIcon(new ImageIcon(ElementsManager.retrieveTypeIcon(element.type)));

		lblTitle = new JLabel(element.name);
		lblTitle.setFont(new Font("Agency FB", Font.BOLD, 23));
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.fill = GridBagConstraints.BOTH;
		gbc_lblTitle.gridwidth = 3;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 0;
		lblTitle.setText(element.name);
		add(lblTitle, gbc_lblTitle);

		lblUsername = new JLabel("");
		lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUsername.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("ElementView.1")+element.userName+"</b></i><HTML>");
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.anchor = GridBagConstraints.SOUTH;
		gbc_lblUsername.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
		gbc_lblUsername.gridx = 3;
		gbc_lblUsername.gridy = 1;
		add(lblUsername, gbc_lblUsername);


		GridBagConstraints gbc_btnSender = new GridBagConstraints();
		gbc_btnSender.gridheight = 2;
		gbc_btnSender.anchor = GridBagConstraints.SOUTHEAST;
		gbc_btnSender.insets = new Insets(0, 0, 5, 0);
		gbc_btnSender.gridx = 4;
		gbc_btnSender.gridy = 0;

		btnSender = new JButton("");
		btnSender.addActionListener(openProfileListener);
		btnSender.setFont(new Font("Dialog", Font.BOLD, 12));
		btnSender.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnSender.setContentAreaFilled(false);
		btnSender.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		btnSender.setHorizontalTextPosition(SwingConstants.CENTER);
		btnSender.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnSender.setIcon(element.senderImage);
		add(btnSender, gbc_btnSender);

		lblPostPhoto = new JLabel("");
		lblPostPhoto.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		GridBagConstraints gbc_lblPostPhoto = new GridBagConstraints();
		gbc_lblPostPhoto.gridwidth = 2;
		gbc_lblPostPhoto.anchor = GridBagConstraints.NORTH;
		gbc_lblPostPhoto.insets = new Insets(0, 0, 5, 5);
		gbc_lblPostPhoto.gridx = 0;
		gbc_lblPostPhoto.gridy = 2;
		add(lblPostPhoto, gbc_lblPostPhoto);
		if (element.photo == null)
			lblPostPhoto.setVisible(false);
		else
			lblPostPhoto.setIcon(element.photo);

		scrollPane = new JScrollPane();
		scrollPane.setMinimumSize(new Dimension(25, 100));
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setOpaque(false);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 2;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setOpaque(false);
		scrollPane.setViewportView(textArea);
		textArea.setMinimumSize(new Dimension(4, 300));
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setText(element.body);

		lblDate = new JLabel("date");
		lblDate.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblDate = new GridBagConstraints();
		gbc_lblDate.gridwidth = 4;
		gbc_lblDate.anchor = GridBagConstraints.SOUTH;
		gbc_lblDate.insets = new Insets(0, 0, 0, 5);
		gbc_lblDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDate.gridx = 0;
		gbc_lblDate.gridy = 3;
		lblDate.setText(ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("ElementView.2")+DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(element.pubblicationTime));
		add(lblDate, gbc_lblDate);

		btnSeleziona = new EmptyContentAreaButton(ElementView.class.getResource("/images/stdIcons24/monitor.png"), ResourceBundle.getBundle("spotGUI.ElementViewer.messages").getString("ElementView.3"));
		btnSeleziona.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnSeleziona.setHorizontalTextPosition(SwingConstants.RIGHT);
		btnSeleziona.addActionListener(selectElementListener);
		GridBagConstraints gbc_btnEspandi = new GridBagConstraints();
		gbc_btnEspandi.anchor = GridBagConstraints.SOUTH;
		gbc_btnEspandi.gridx = 4;
		gbc_btnEspandi.gridy = 3;
		add(btnSeleziona, gbc_btnEspandi);

	}


	private void setPreferredSize(Element element) {
		if (element.photo != null) {
			int h = Math.max(125 + element.photo.getIconHeight(), 170);
			setPreferredSize(new Dimension(500, h));
		}
		else
			setPreferredSize(new Dimension(500, 170));
	}

}
